<?php
$id = $_GET['id'];
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
$db = new Database();
//select record from tbredeem
$getLastTransaction = $db->query("SELECT * FROM tbredeem WHERE trans_id='$id'","array");
foreach($getLastTransaction as $row)
{
        $dbProdID = $row['product_id'];
        $idQuantity = $row['quantity'];
        $totalPoints = $row['total'];
        $dealerCode = $row['dealerCode'];
        $productType = $row['product_type'];
        $dates = $row['date'];
}

//select name from tbdealer
$name = $db->query("SELECT CONCAT(lastName,', ',firstName) as name from tbdealer where dealerCode='$dealerCode' GROUP BY dealerCode");
foreach($name as $dealerName){
        $dealerNames = $dealerName['name'];
}
$getPoints = $db->query("SELECT * FROM tbdealer WHERE dealerCode='$dealerCode' and expiryDate > CURDATE()","array");
foreach($getPoints as $data)
{
    $dealerPoints = $data['pointsEarned'];
    foreach (extractForms($dealerPoints) as $key => $value) {
        if(isset($total[$key]))
            $total[$key] += $value; 
        else
            $total[$key] = $value;
    }
}
$pdf->Ln();
//for the logo
$getSettings = $db->query("SELECT * FROM tbsettings");
foreach($getSettings as $value){
    $Invoice = $value['UpdateInvoice'];
    $Product = $value['UpdateProduct'];
    $logo = $value['logo'];
    $SystemName = $value['SystemName'];
    $footer = $value['footer'];
}
$pdf->Image("images/logo/logo.png",10,10,70);
$pdf->Ln();
$pdf->Cell(10,25,"");
$pdf->Ln();
$pdf->Cell(140,15,"Name:  $dealerNames");
$pdf->Cell(10,15,"Date:  $dates");
$pdf->Ln();
// Colors, line width and bold font
$pdf->SetFillColor(255,0,150);
$pdf->SetTextColor(255);
$pdf->SetDrawColor(255,0,150);
$pdf->SetLineWidth(.3);
$pdf->SetFont('','B');
//header of the table
$cttr = 0;
$header = array("Product","Points","Quantity","Total");
foreach($header as $col){
        $cttr++;
        if($cttr>1){
                $pdf->Cell(20,7,$col,1,0,'C',true);        
        }else{
                $pdf->Cell(128,7,$col,1,0,'C',true);
        }
}
$pdf->Ln();
$pdf->SetFillColor(224,235,255);
$pdf->SetTextColor(0);
$pdf->SetFont(''); //set color
//$ctr = 0; //variable for transaction number
//get all the redeem products from tbredeem then explode the product and quantity field
$getSum = $db->query("SELECT SUM(total) as totalRedeem FROM tbredeem WHERE dealerCode='$dealerCode'","row");

$getLastTransaction = $db->query("SELECT * FROM tbredeem WHERE trans_id='$id'","array"); 
foreach($getLastTransaction as $row){
        $red_id =  $row['product_id'];
        $quantity =  $row['quantity'];
        $remaining_points = $row['remaining_points'];
        $type = $row['product_type'];
        //Get Description of the type in product line
        $getProductLines = $db->query("SELECT * FROM tbproduct_line WHERE code='$type' AND is_active='1'","array");
        foreach($getProductLines as $productLine){
            $code = $productLine['code'];
            $description = $productLine['description'];
        }
        
        //$getProd = $db->query("SELECT * FROM tbproduct where id = ");
        $prodID_explode = explode(",",$red_id);
        $quantity_explode = explode(",",$quantity);
        //$ctr++; //increment transaction #
        $pdf->SetFont( 'Arial', 'B');
        if($code==$type){
            $pdf->Cell(128,6,"Transaction $description",'LR',0,'L',$fill);
        }else{
            $pdf->Cell(128,6,"Transaction $type",'LR',0,'L',$fill);
        }
        $pdf->SetFont('');
        $pdf->Cell(20,6,"",'LR',0,'L',$fill);
        $pdf->Cell(20,6,"",'LR',0,'L',$fill);
        $pdf->Cell(20,6,"",'LR',0,'L',$fill);
        $pdf->Ln();
        //after explodeing select all products from tbproduct
        for($a=0;$a<count($quantity_explode);$a++){
                $query = $db->query("SELECT * FROM tbproduct where id='$prodID_explode[$a]'","row"); //get the product from
                $pdf->Cell(188,0,'','T'); // end border
                $pdf->Ln();
                $pdf->Cell(128,6,$query['product_name'],'LR',0,'L',$fill);
                $pdf->Cell(20,6,$query['points'],'LR',0,'L',$fill);
                $pdf->Cell(20,6,$quantity_explode[$a],'LR',0,'L',$fill);
                $pdf->Cell(20,6,$quantity_explode[$a]*$query['points'],'LR',0,'L',$fill);
                $totals+=$quantity_explode[$a]*$query['points'];
                $pdf->Ln();
                $pdf->Cell(188,0,'','T'); // end border
                $pdf->Ln();
        }
        // for total redeemed points
        $pdf->SetFont( 'Arial', 'I');
        $pdf->Cell(128,6,"Total Redeemed Points:    $totals",'LR',0,'L',$fill);
        $pdf->SetFont('');
        $pdf->Cell(20,6,"",'LR',0,'L',$fill);
        $pdf->Cell(20,6,"",'LR',0,'L',$fill);
        $pdf->Cell(20,6,"",'LR',0,'L',$fill);
        $pdf->Ln();
        // for total remaining points
        $pdf->Cell(128,6,"Remaining Points:    $remaining_points",'LR',0,'L',$fill);
        $count++;
        $pdf->Cell(20,6,"",'LR',0,'L',$fill);
        $pdf->Cell(20,6,"",'LR',0,'L',$fill);
        $pdf->Cell(20,6,"",'LR',0,'L',$fill);
        $pdf->Ln();
        //$points = $db->query("SELECT * FROM tbredeem WHERE")
        $pdf->Cell(128,6,"",'LR',0,'L',$fill);
        $pdf->Cell(20,6,"",'LR',0,'L',$fill);
        $pdf->Cell(20,6,"",'LR',0,'L',$fill);
        $pdf->Cell(20,6,"",'LR',0,'L',$fill);
        $fill = !$fill;
        $pdf->Ln();
        $totals = 0; //back to 0 the total redeemed points counter per transaction
}
$pdf->Cell(188,0,'','T'); // end border

$pdf->Ln(); // next line
$pdf->Cell(118,50,"");$pdf->Cell(40,50,"");
$pdf->Ln(); // next line
$pdf->Cell(118,15,"Issued by:");$pdf->Cell(40,15,"Received by:");
$pdf->Ln(); // next line
$pdf->Cell(110,0,"_________________________________");$pdf->Cell(40,0,"_________________________________");
$pdf->Ln(); // next line
$pdf->Cell(20,10,"");$pdf->Cell(110,10,"Name and Signature");$pdf->Cell(40,10,"Name and Signature");
$pdf->Ln(); // next line
$pdf->Output(); //outputing the pdf to the browser
?>